package com.ruoyi.portal.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.portal.domain.PorNews;
import com.ruoyi.portal.service.IPorNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 新闻Controller
 *
 * @author cjie
 * @date 2021-05-21
 */
@Api(tags = " 新闻")
@RestController
@RequestMapping("/news")
public class PorNewsController extends BaseController {
    @Autowired
    private IPorNewsService porNewsService;

    /**
     * 查询新闻列表
     */
    @ApiOperation(value = "查询新闻列表", notes = "查询新闻列表")
    @GetMapping("/list")
    public TableDataInfo list(PorNews porNews) {
        startPage();
        List<PorNews> list = porNewsService.selectPorNewsList(porNews);
        return getDataTable(list);
    }
    /**
     * 首页查询新闻列表
     */
    @ApiOperation(value = "首页查询新闻列表", notes = "查询新闻列表")
    @ApiImplicitParam(name = "labels",value = "新闻标签", required = true, allowMultiple = true, paramType = "path", dataType = "String")
    @PostMapping("/listByLabels")
    public TableDataInfo listByLabels(String[] labels) {
        startPage();
        List<PorNews> list = porNewsService.selectPorNewsByLabels(labels);
        return getDataTable(list);
    }

    /**
     * 导出新闻列表
     */
    @ApiOperation(value = "导出新闻列表", notes = "导出新闻列表")
    @PreAuthorize(hasPermi = "portal:news:export")
    @Log(title = "新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PorNews porNews) throws IOException {
        List<PorNews> list = porNewsService.selectPorNewsList(porNews);
        ExcelUtil<PorNews> util = new ExcelUtil<PorNews>(PorNews.class);
        util.exportExcel(response, list, "新闻数据");
    }

    /**
     * 获取新闻详细信息
     */
    @ApiOperation(value = "获取新闻详细信息", notes = "获取新闻详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(porNewsService.selectPorNewsById(id));
    }

    /**
     * 新增新闻
     */
    @ApiOperation(value = "新增新闻", notes = "新增新闻")
    @PreAuthorize(hasPermi = "portal:news:add")
    @Log(title = "新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PorNews porNews) {
        return toAjax(porNewsService.insertPorNews(porNews));
    }

    /**
     * 修改新闻
     */
    @ApiOperation(value = "修改新闻", notes = "修改新闻")
    @PreAuthorize(hasPermi = "portal:news:edit")
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PorNews porNews) {
        return toAjax(porNewsService.updatePorNews(porNews));
    }

    /**
     * 删除新闻
     */
    @ApiOperation(value = "删除新闻", notes = "删除新闻")
    @ApiImplicitParam(name = "ids", value = "新闻ID数组", required = true, paramType = "path", allowMultiple = true, dataType = "String")
    @PreAuthorize(hasPermi = "portal:news:remove")
    @Log(title = "新闻", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(porNewsService.deletePorNewsByIds(ids));
    }
}
